package com.beauty.dressing.module.main.activity;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.view.ViewPager;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageButton;

import com.beauty.dressing.R;
import com.beauty.dressing.common.base.BaseFragment;
import com.beauty.dressing.common.bean.Video;
import com.beauty.dressing.common.utils.NetTools;
import com.beauty.dressing.common.video.VideoPlayer;
import com.beauty.dressing.common.view.NoScrollViewPager;
import com.beauty.dressing.module.main.fragment.CourseFragment;
import com.beauty.dressing.module.main.fragment.MyCourseFragment;
import com.beauty.dressing.module.main.fragment.VideoCommentFragment;
import com.beauty.dressing.module.main.fragment.VideoInListFragment;
import com.beauty.dressing.module.main.fragment.VideoIntroductFragment;
import com.framework.common.base.IBaseFragment;
import com.framework.common.base.IFragmentPagerAdapter;
import com.framework.common.utils.ICommonUtil;
import com.framework.common.utils.IDisplayUtil;
import com.framework.common.utils.ILog;
import com.framework.common.view.tablayout.CommonTabLayout;
import com.framework.common.view.tablayout.bean.BaseTabEntity;
import com.framework.common.view.tablayout.listener.CustomTabEntity;
import com.framework.common.view.tablayout.listener.OnTabSelectListener;

import java.util.ArrayList;
import java.util.List;

/**
 * @author qinbaowei
 * @createtime 2015年11月16日 15:14
 * @company 9zhitx.com
 * @description 视频播放页面
 */

public class VideoPlayActivity extends BaseVideoPlayerActivity implements OnTabSelectListener {
    /**
     * 传值标识符
     */
    private static final String TAG_VIDEO = "video";
    private static final int TAG_DETAIL = 0;
    private static final int TAG_VIDEO_LIST = 1;
    private static final int TAG_COMMENT = 2;

    /**
     * fragment 标识
     */
    public static final int FRAGMENT_DETAIL = 0;
    public static final int FRAGMENT_INTROUCTION = 1;
    public static final int FRAGMENT_RELATED = 2;
//    public static final int FRAGMENT_COMMENT = 3;
    public static final int FRAGMENT_VOTED = 3;
    public static final int FRAGMENT_LOVED = 4;

    private boolean hasCollectStatusChange;
    private CommonTabLayout mTabLayout;
    public NoScrollViewPager mViewPager;
    private BaseFragment[] fragments;
    @Override
    public void setActivityVideo(Video video) {
        if (null == video) {
            ILog.le("[vp]param video is null");
            return;
        }
        ILog.ld("[vp]old video %s, new video %s", null == mVideo ? "" : mVideo.title, video.title);
        // 原始视频信息不全， 获取详情后设置视频时，播放视频
        if (null == mVideo || TextUtils.isEmpty(mVideo.videoUrl) || mVideo.id != video.id || !mVideo.videoUrl.equals(video.videoUrl)) {
            ILog.ld("[vp]here 1");
            mVideo = video;
            initVideoPlayerAd();
            mVideoPlayer.init(this, this, null);
            mVideoPlayer.play();
        } else {
            ILog.ld("[vp]here 2");
            if (null != mVideo.videoAd) {//保留原视频的广告
                video.videoAd = mVideo.videoAd;
            }
            mVideo = video;
        }
    }


//    private ShareDialog shareDialog = null;

    /**
     * 视频播放统一入口
     *
     * @param context
     * @param video
     */
    public static void startActivity(Context context, Video video) {
        Intent intent = new Intent(context, VideoPlayActivity.class);
        if (video != null) {
            intent.putExtra(TAG_VIDEO, video);
        }
        context.startActivity(intent);
    }

    @Override
    public void setContentView() {
        changeSystemBarColor = false;
        setContentView(R.layout.activity_play_video);
    }

    @Override
    public void findView() {
        mBackBtn = (ImageButton) findViewById(R.id.back_btn);
        mBackBtn.setOnClickListener(this);
        mVideoPlayer = (VideoPlayer) findViewById(R.id.video_player_controller);
        mVideoPlayer.getLayoutParams().height = (int) (IDisplayUtil.getScreenWidth(this)*0.5625f);
        mTabLayout = (CommonTabLayout) findViewById(R.id.tab_layout);
        mViewPager = (NoScrollViewPager) findViewById(R.id.viewpager);
        mTabLayout.setOnTabSelectListener(this);
        ArrayList<CustomTabEntity> list = new ArrayList<>();
        list.add(new BaseTabEntity("详情"));
        list.add(new BaseTabEntity("目录"));
        list.add(new BaseTabEntity("评价(5)"));
        mTabLayout.setTabData(list);
    }

    @Override
    public void initData() {
        super.initData();
        fragments = new BaseFragment[3];
        fragments[TAG_DETAIL] = new VideoIntroductFragment();
        fragments[TAG_VIDEO_LIST] = new VideoInListFragment();
        fragments[TAG_COMMENT] = new VideoCommentFragment();
        List<BaseFragment> fragmentList = new ArrayList<>();
        fragmentList.add(fragments[TAG_DETAIL]);
        fragmentList.add(fragments[TAG_VIDEO_LIST]);
        fragmentList.add(fragments[TAG_COMMENT]);
        IFragmentPagerAdapter mPagerAdapter = new IFragmentPagerAdapter(getSupportFragmentManager());
        mPagerAdapter.setList(fragmentList);
        mViewPager.setAdapter(mPagerAdapter);
        mViewPager.setOffscreenPageLimit(fragmentList.size());
        mViewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position != mTabLayout.getCurrentTab()) {
                    mTabLayout.setCurrentTab(position);
                    onTabFocusChange(position);
                }
            }

            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        Video video = null;
        Intent intent = getIntent();
        if (intent != null && intent.hasExtra(TAG_VIDEO)) {
            video = (Video) intent.getSerializableExtra(TAG_VIDEO);
        }
        video = new Video();
        video.id = 10000;
        video.videoUrl = "http://clips.vorwaerts-gmbh.de/big_buck_bunny.mp4";
        if (null != video) {
            //如果传进来的视频对象只有id，就不播放，否则会提示播放失败，播放详情获取到视频播放信息之后会正常播放，导致短暂的显示一下播放错误
            if (!TextUtils.isEmpty(video.videoUrl)) {
                setActivityVideo(video);
            } else {
                ILog.lw("[vp]Video %s only exist id, so ignore play", video.title);
                mVideo = video;
            }
        }

    }

    private void initVideoPlayerAd() {
        //视频播放地址为空，但是却有广告，视频详情接口成功之后又会重新设置播放，导致播放错误
        //所以视频播放地址不为空的时候就加广告，否则不加
        if (null != mVideo && !TextUtils.isEmpty(mVideo.videoUrl) && null == mVideo.videoAd) {//如果有广告就不在重新获取
            ILog.ld("[vp]set video ad");
            mVideo.videoAd = null;
        }
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.back_btn:
                onBackPressed();
                break;

            default:
                break;
        }
    }


    @Override
    protected View getRootView() {
        return findViewById(R.id.root);
    }

    @Override
    public void onBackPressed() {
        super.onBackPressed();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }

    public void onShareClick() {
        if (ICommonUtil.isFastDoubleClick()) {
            return;
        }
        if (mVideo == null) {
            return;
        }
        if (!NetTools.isNetAvailble(this)) {
            showToast(R.string.no_network_to_remind);
            return;
        }
        ILog.ld("video share url %s", mVideo.videoShareUrl);
        int orientation = getResources().getConfiguration().orientation;
        switch (orientation) {
            case Configuration.ORIENTATION_PORTRAIT:
                //                shareDialog = new ShareDialog(this, mVideo.title, ShareDialog.getCheckLengthContent(mVideo.introduction), mVideo.videoShareUrl, mVideo, mVideo.getShareType(), mVideo.id, false, null);
                break;
            case Configuration.ORIENTATION_LANDSCAPE:
                break;
            default:
                break;
        }
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    @Override
    public void onTabSelect(int position) {
        if (position != mViewPager.getCurrentItem()) {
            mViewPager.setCurrentItem(position, true);
        }
        onTabFocusChange(position);
    }

    public boolean isFirstPageSelect(){
        return mViewPager!=null && mViewPager.getCurrentItem()== TAG_DETAIL;
    }
    private int lastSelection;
    private void onTabFocusChange(int position){
        lastSelection = position;
    }

    @Override
    public void onTabReselect(int position) {

    }

//    public void refreshVideoDetailFragment(Comment comment) {
//        VideoDetailFragment videoDetailFragment = (VideoDetailFragment) fragments[FRAGMENT_DETAIL];
//        if (videoDetailFragment != null) {
//            videoDetailFragment.commentSuccess(comment);
//        }
//    }
}
